package com.citicpub_cloud.restfull.server.user;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.citicpub_cloud.restfull.api.dto.TbAdminAccountExt;
import com.citicpub_cloud.restfull.common.constant.AdminUserConstant;
import com.citicpub_cloud.restfull.common.http.request.AuthServerRequest;
import com.citicpub_cloud.restfull.common.http.response.CommonResponse;
import com.citicpub_cloud.restfull.common.http.response.ResponseCode;
import com.citicpub_cloud.restfull.server.BaseServer;
import com.citicpub_cloud.restfull.service.AdminUserService;
import com.citicpub_cloud.restfull.utils.Base64Util;
import com.citicpub_cloud.restfull.utils.SessionUtils;

/**
 * @Description： 后端用户相关控制器
 * @date: 2016年9月6日 下午4:29:10 
 * @author zhangyufeng@citicpub.com
 */
@Controller
@RequestMapping("/user")
public class AdminUserServer extends BaseServer {

	

	/**
	 * adminUserService:后端用户服务接口
	 */
	@Autowired
	private AdminUserService adminUserService;
	

	/**
	 * @param session
	 * @param request
	 * @return
	 * String
	 */
	@RequestMapping(value = "/login")
	public @ResponseBody String login(HttpSession session,HttpServletRequest req,@RequestBody AuthServerRequest request, HttpServletResponse response) {
		CommonResponse commonResp = new CommonResponse();
		String login = request.getLoginName();
		String pwd = request.getPwd();
		Map<String, String> map = new HashMap<String, String>();
		
		try {
			// 1.判断用户名是否为空
			if (StringUtils.isBlank(login)) {
				log.debug("用户名不能为空");
				commonResp.getHeader().setCode(ResponseCode.PARAMETER_ERROR.getStatcode());
				commonResp.getHeader().setMessage("用户名不能为空");
				return commonResp.toJson();
			}
			// 2.判断密码是否为空
			if (StringUtils.isBlank(pwd)) {
				log.debug("密码不能为空");
				commonResp.getHeader().setCode(ResponseCode.PARAMETER_ERROR.getStatcode());
				commonResp.getHeader().setMessage("密码不能为空");
				return commonResp.toJson();
			}
			
			
			
			TbAdminAccountExt userAccount = adminUserService.login(login, pwd);
			if (null != userAccount) {
				String token = Base64Util.encode(UUID.randomUUID().toString().getBytes(Charset.forName("UTF-8")));
				String key = SessionUtils.getRedisSessionKeyForAdmin(AdminUserConstant.SessionConstant.REDIS_SESSION_TAG_NAME, userAccount.getUid(), token);
			
				redisDao.set(key, userAccount, this.getSessionTimeout());
				commonResp.getHeader().setCode(ResponseCode.SUCCESS.getStatcode());
				commonResp.getHeader().setMessage("登录成功");
				
				map.put("uid", userAccount.getUid());
				map.put("token", token);
				commonResp.setBody(map);
				
				return commonResp.toJson();
			}
		} catch (Exception e) {
			log.error("认证用户出错",e);			
		}
		
		commonResp.getHeader().setCode(ResponseCode.FAILED.getStatcode());
		commonResp.getHeader().setMessage("失败");
		return commonResp.toJson();
	}


	/**
	 * @desccroption:退出登录
	 * @param session
	 * @param request
	 * @return
	 * String
	 */
	@RequestMapping(value = "/logout")
	public @ResponseBody String logout(HttpSession session,@RequestBody AuthServerRequest request) {
		if (null != session && null != session.getAttribute(AdminUserConstant.SessionConstant.SESSION_ATTR_NAME)) {
			session.invalidate();
		}
		String uid = request.getUid();
		String token = request.getToken();
		String key = SessionUtils.getRedisSessionKeyForAdmin(AdminUserConstant.SessionConstant.REDIS_SESSION_TAG_NAME, uid, token);
		if (StringUtils.isNotBlank(key)) {
			redisDao.expire(key, 1);
		}
		CommonResponse commonResp = new CommonResponse();
		Map<String, Object> map = new HashMap<String, Object>();
		commonResp.getHeader().setCode(ResponseCode.SUCCESS.getStatcode());
		commonResp.getHeader().setMessage("成功");
		return commonResp.toJson();
	}
}
